package wcg;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;


public class DlgReceiveUDPSetting extends JDialog {

	private JPanel contentPanel = new JPanel();
	private JTextField textFieldPort;
	private JCheckBox chckbxNewCheckBoxRevceiveUdp=null;
	
	private int Port=7200;
	private boolean RecvUdp=false;
	public boolean ReturnCode = false;
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		try {
//			DlgReceiveUDPSetting dialog = new DlgReceiveUDPSetting();
//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//			dialog.setVisible(true);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Create the dialog.
	 */
	public DlgReceiveUDPSetting() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(DlgReceiveUDPSetting.class.getResource("/images/server_connect.png")));
		setTitle("Reveive UDP");
		setBounds(100, 100, 173, 171);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		chckbxNewCheckBoxRevceiveUdp = new JCheckBox("Receive UDP");
		chckbxNewCheckBoxRevceiveUdp.setToolTipText("Receive WIN A0 UDP or ACT packet");
		chckbxNewCheckBoxRevceiveUdp.setBounds(7, 18, 106, 21);
		contentPanel.add(chckbxNewCheckBoxRevceiveUdp);
		
		textFieldPort = new JTextField();
		textFieldPort.setToolTipText("Specify UDP Port ");
		textFieldPort.setBounds(61, 45, 82, 26);
		contentPanel.add(textFieldPort);
		textFieldPort.setColumns(10);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(7, 45, 42, 26);
		contentPanel.add(lblPort);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						onOK();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						onCancel();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	private void onOK(){
		ReturnCode = true;
		setVisible(false);
		dispose();
	}
	private void onCancel(){
		ReturnCode = false;
		setVisible(false);
		dispose();
	}
	
	public void doDataExchange(boolean get){
		if(get){
			String p = textFieldPort.getText();
			p = p.trim();
			Port = Integer.valueOf(p);
			if(chckbxNewCheckBoxRevceiveUdp.isSelected()){
				RecvUdp = true;
			}
			else{
				RecvUdp = false;
			}
		}
		else{
			textFieldPort.setText(String.valueOf(Port));
			chckbxNewCheckBoxRevceiveUdp.setSelected(RecvUdp);
		}
	}
	public int getPort() {
		return Port;
	}
	public boolean isRecvUdp() {
		return RecvUdp;
	}
	public void setPort(int port) {
		Port = port;
	}
	public void setRecvUdp(boolean recvUdp) {
		RecvUdp = recvUdp;
		if(RecvUdp){
			textFieldPort.setEditable(false);
		}
		else{
			textFieldPort.setEditable(true);
		}
	}

}
